//
//  History.m
//  CocOCaml
//
//	$Id: History.m,v 1.1.1.1 2003/02/27 16:53:39 wneuman Exp $
//
//  Created by William D. Neumann on Thu Jan 16 2003.
//  Copyright (c) 2003 William D. Neumann. All rights reserved.
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the Artistic License as published by Larry
//	Wall, either version 2.0, or (at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	Artistic License for more details.
//
//	You should have received a copy of the Artistic License with this Kit,
//	in the file named "Artistic.clarified".  If not, I'll be glad to provide
//	one.
//

#import "History.h"


@implementation History
- (id) init
{
    if (self = [super init]) {
        currentHistory = [[NSMutableArray alloc] initWithCapacity:128];
        lexicographic = NO;
        lexicoSorted = NO;
        lexicoHint = nil;
        currentItem = 0;
    }
    return self;
}

- (void) addEvent:(HistoryElement *)newElement
{
    [currentHistory addObject:newElement];
    currentItem = [currentHistory count];
}

- (NSArray *) getChronological
{
    NSArray* sortedArray = [currentHistory sortedArrayUsingSelector:@selector(compareIndex:)];
    return sortedArray;
}

- (NSArray *) getLexicographic
{
    NSArray* sortedArray = [currentHistory sortedArrayUsingSelector:@selector(compareText:)];
    return sortedArray;
}

- (int) count
{
    return [currentHistory count];
}

- (bool) isLexicographic
{
    return lexicographic;
}

- (bool) isLexicoSorted
{
    return lexicoSorted;
}

- (void) toggleSort
{
    lexicographic = !lexicographic;
}

- (NSString *) nextHistoryItem
{
    NSArray * theHistory;

    if (!lexicographic)
        theHistory = currentHistory;
    else
        theHistory = [self getLexicographic];

    if (currentItem == [theHistory count]-1) currentItem--;
    NSString * nextItem = [[theHistory objectAtIndex:++currentItem] getText];

    return nextItem;
}

- (NSString *) previousHistoryItem
{
    NSArray * theHistory;

    if (!lexicographic)
        theHistory = currentHistory;
    else
        theHistory = [self getLexicographic];

    if (currentItem == 0) currentItem = 1;
    NSString * prevItem = [[theHistory objectAtIndex:--currentItem] getText];

    return prevItem;
}

- (unsigned int) getCurrentItem
{
    return currentItem;
}

- (void) dealloc
{
    if (lexicoHint) [lexicoHint release];
    [currentHistory release];
    [super dealloc];
}

@end
